package vivo.com.foolishbar.core.warp.page;

import javax.persistence.EntityManager;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wideplay.warp.annotations.OnEvent;
import com.wideplay.warp.annotations.Page;
import com.wideplay.warp.annotations.URIMapping;
import com.wideplay.warp.persist.Transactional;

@URIMapping("/blogs/compose")
public class ComposeBlog {

	@Inject
	@Page
	private ListBlogs listBlogsPage;

	@Inject
	private Provider<EntityManager> em;

	private Blog newBlog = new Blog("", ""); // an empty blog

	@OnEvent
	@Transactional
	public ListBlogs save() {
		// save the new blog!
		if (!em.get().contains(newBlog)) {
			System.out.println("merge");
			newBlog = em.get().merge(newBlog);
		}
		em.get().persist(newBlog);

		// return to the list page
		return listBlogsPage;
		// Other post and redirect methods.
		// Redirect.toPage(listBlogs);
		// return Redirect.to("http://www.wideplay.com");
		// return Redirect.to("/Start");
		// return Redirect.to("underMe/content.html");
	}

	public Blog getNewBlog() {
		return newBlog;
	}

	public String getAbc() {
		return "abcdd";
	}
}
